package dao.mapMysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IObject;
import modelInterfaces.ISubject;
import dao.DAOException;
import dao.IObjectDAO;

/**
 *
 * @author salvador
 */
public class ObjectMapMySqlDAO implements IObjectDAO {


	@Override
	public List<IObject> retrieveAll() throws DAOException {
		ResultSet rs = MapMySqlDBConnection.getInstance().executeQuery("SELECT object FROM event GROUP BY object ORDER BY object");
        List<IObject> subjectList = new ArrayList<IObject>();
        try
        {
            while (rs.next())
            {
            	IObject object = AbstractModelFactory.getModelFactory().getItem();
                object.setId(rs.getInt("object"));
                subjectList.add(object);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return subjectList;
    }

	@Override
	public List<IObject> retrieveAllExcept(IObject object) throws DAOException {
		ResultSet rs = MapMySqlDBConnection.getInstance().executeQuery("SELECT object FROM event where object != '"+object.getId()+"' GROUP BY object ORDER BY object");
        List<IObject> distinctObjectList = new ArrayList<IObject>();
        try
        {
            while (rs.next())
            {
            	IObject distinctObject = AbstractModelFactory.getModelFactory().getItem();
                distinctObject.setId(rs.getInt("subject"));
                distinctObjectList.add(distinctObject);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return distinctObjectList;
	}

	@Override
	public void create(IObject object) throws DAOException {
		// TODO [Andy] Auto-generated method stub
		
	}

	@Override
	public void delete(IObject object) throws DAOException {
		// TODO [Andy] Auto-generated method stub
		
	}

	@Override
	public void update(IObject object) throws DAOException {
		// TODO [Andy] Auto-generated method stub
		
	}


	@Override
	public List<IObject> retrieveAllFromSubject(ISubject subject) throws DAOException {
		ResultSet rs = MapMySqlDBConnection.getInstance().executeQuery("SELECT object FROM event GROUP BY object ORDER BY object");
        List<IObject> subjectList = new ArrayList<IObject>();
        try
        {
            while (rs.next())
            {
            	IObject object = AbstractModelFactory.getModelFactory().getItem();
                object.setId(rs.getInt("object"));
                subjectList.add(object);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return subjectList;
	}
	
	
}
